﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PlotModelExtensions.cs" company="OxyPlot">
//   Copyright (c) 2014 OxyPlot contributors
// </copyright>
// <summary>
//   Provides reporting extension methods to the <see cref="PlotModel" />.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace OxyPlot.Reporting
{
    using System.Collections.Generic;
    using System.Globalization;
    using System.IO;
    using System.Reflection;

    using OxyPlot.Axes;
    using OxyPlot.Series;

    /// <summary>
    /// Provides reporting extension methods to the <see cref="PlotModel" />.
    /// </summary>
    public static class PlotModelExtensions
    {
        /// <summary>
        /// Creates a report for the plot.
        /// </summary>
        /// <param name="plotModel">The plot model.</param>
        /// <returns>A report.</returns>
        public static Report CreateReport(this PlotModel plotModel)
        {
            var r = new Report { Culture = CultureInfo.InvariantCulture };

            r.AddHeader(1, "P L O T   R E P O R T");
            r.AddHeader(2, "=== PlotModel ===");
            r.AddPropertyTable("PlotModel", plotModel);

            r.AddHeader(2, "=== Axes ===");
            foreach (Axis a in plotModel.Axes)
            {
                r.AddPropertyTable(a.GetType().Name, a);
            }

            r.AddHeader(2, "=== Annotations ===");
            foreach (var a in plotModel.Annotations)
            {
                r.AddPropertyTable(a.GetType().Name, a);
            }

            r.AddHeader(2, "=== Series ===");
            foreach (var s in plotModel.Series)
            {
                r.AddPropertyTable(s.GetType().Name, s);
                var ds = s as DataPointSeries;
                if (ds != null)
                {
                    var fields = new List<ItemsTableField> { new ItemsTableField("X", "X"), new ItemsTableField("Y", "Y") };
                    r.AddItemsTable("Data", ds.Points, fields);
                }
            }

#if UNIVERSAL
            var assemblyName = new AssemblyName(typeof(PlotModel).GetTypeInfo().Assembly.FullName);
#else
            var assemblyName = new AssemblyName(Assembly.GetExecutingAssembly().FullName);
#endif
            r.AddParagraph(string.Format("Report generated by OxyPlot {0}", assemblyName.Version.ToString(3)));

            return r;
        }

        /// <summary>
        /// Creates a text report for the plot model.
        /// </summary>
        /// <param name="plotModel">The plot model.</param>
        /// <returns>A text report that contains information about the contents of the plot model.</returns>
        public static string CreateTextReport(this PlotModel plotModel)
        {
            using (var ms = new MemoryStream())
            {
                var trw = new TextReportWriter(ms);
                Report report = plotModel.CreateReport();
                report.Write(trw);
                trw.Flush();
                ms.Position = 0;
                var r = new StreamReader(ms);
                return r.ReadToEnd();
            }
        }
    }
}
